package com.example.inventoryservice.service;

import com.example.inventoryservice.feign.StockFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class InventoryService {

    private static final Map<String, Integer> inventoryMap = new HashMap<>();

    static {
        inventoryMap.put("product1", 100);
        inventoryMap.put("product2", 200);
    }

    @Autowired
    private StockFeignClient stockFeignClient;

    public Integer getInventory(String productId) {
        return inventoryMap.getOrDefault(productId, 0);
    }

    public Boolean deductInventory(String productId, Integer count) {
        if (inventoryMap.getOrDefault(productId, 0) >= count) {
            inventoryMap.put(productId, inventoryMap.get(productId) - count);
            // 调用存货服务记录扣减
            stockFeignClient.recordDeduct(productId, count);
            return true;
        }
        return false;
    }
}
    